<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Admin\Controller;
use User\Api\UserApi;

/** 
 * 后台用户控制器
 * @author 麦当苗儿 <zuojiazi@vip.qq.com>
 */
class StuffController extends AdminController { 
    /**
     * 供应商
     * @author 麦当苗儿 <zuojiazi@vip.qq.com>
     */
    /*列表页 20180206  by  zhanghao*/ 
    public function lists(){   
        $map['status']  =   array('egt',0);  
        $list = M('member')->where($map)->select();  
        $this->assign('_list', $list);
        $this->display();  
    } 
    /*新增行为 20180206  by  zhanghao*/ 
    public function addAction(){
        $this->meta_title = '新增行为';
        $this->assign('data',null);
        $this->display('editaction');
    } 
    /*新增用户 20180206  by  zhanghao*/
    public function add(){ 
         $data['username'] = $_POST['username'];       
         $data['password'] = $_POST['password'];       
         $data['repassword'] = $_POST['repassword'];       
         $data['email'] = $_POST['email'];   
        if(IS_POST){
            /* 检测密码 */
            if($data['password'] != $data['repassword']){
                $this->ajaxReturn('密码和重复密码不一致!');
                return;
            } 
            /* 调用注册接口注册用户 */
            $User   =   new UserApi;
            $uid    =   $User->register($data['username'], $data['password'], $data['email'],$data['create_time']);
            if(0 < $uid){ //注册成功
                $user = array('uid' => $uid, 'nickname' => $data['username'], 'status' => 1,'create_time'=>time());
                if(!M('Member')->add($user)){
                    $this->ajaxReturn('用户添加失败!');
                } else {
                    $this->ajaxReturn('用户添加成功!');
                }
            } else { //注册失败，显示错误信息
                $this->ajaxReturn($this->showRegError($uid));
            }
        } else {
            $this->meta_title = '新增用户';
            $this->display();
        }
    }  
    /**
     * 获取用户注册错误信息 20180206  by  zhanghao
     * @param  integer $code 错误编码
     * @return string        错误信息
     */ 
    private function showRegError($code = 0){
        switch ($code) {
            case -1:  $error = '用户名长度必须在16个字符以内！'; break;
            case -2:  $error = '用户名被禁止注册！'; break;
            case -3:  $error = '用户名被占用！'; break;
            case -4:  $error = '密码长度必须在6-30个字符之间！'; break;
            case -5:  $error = '邮箱格式不正确！'; break;
            case -6:  $error = '邮箱长度必须在1-32个字符之间！'; break;
            case -7:  $error = '邮箱被禁止注册！'; break;
            case -8:  $error = '邮箱被占用！'; break;
            case -9:  $error = '手机格式不正确！'; break;
            case -10: $error = '手机被禁止注册！'; break;
            case -11: $error = '手机号被占用！'; break;
            default:  $error = '未知错误';
        }
        return $error;
    }   
 
   /*用户状态 20180206  by  zhanghao*/
    public function changeStatus($method=null){
        $id = array_unique((array)I('id',0));
        if( in_array(C('USER_ADMINISTRATOR'), $id)){
            $this->error("不允许对超级管理员执行该操作!");
        }
        $id = is_array($id) ? implode(',',$id) : $id;
        if ( empty($id) ) {
            $this->error('请选择要操作的数据!');
        }
        $map['uid'] =   array('in',$id);
        switch ( strtolower($method) ){
            case 'forbiduser':
                $this->forbid('Member', $map );
                break;
            case 'resumeuser':
                $this->resume('Member', $map );
                break;
            case 'deleteuser':
                $this->delete('Member', $map );
                break;
            default:
                $this->error('参数非法');
        }
    } 
    /*用户行为列表 20180206  by  zhanghao*/
    public function action(){
        //获取列表数据
        $Action =   M('Action')->where(array('status'=>array('gt',-1)));
        $list   =   $this->lists($Action);
        int_to_string($list);
        // 记录当前列表页的cookie
        Cookie('__forward__',$_SERVER['REQUEST_URI']);

        $this->assign('_list', $list);
        $this->meta_title = '用户行为';
        $this->display();
    }
 
     /*新增行为 20180206  by  zhanghao*/
    // public function addAction(){
    //     $this->meta_title = '新增行为';
    //     $this->assign('data',null);
    //     $this->display('editaction');
    // }

    /*编辑行为 20180206  by  zhanghao*/
    public function editAction(){
        $id = I('get.id');
        empty($id) && $this->error('参数不能为空！');
        $data = M('Action')->field(true)->find($id);

        $this->assign('data',$data);
        $this->meta_title = '编辑行为';
        $this->display();
    } 
}
